package com.example.framework.security.core.handler;

import com.example.framework.common.pojo.R;
import com.example.framework.common.util.servlet.ServletUtils;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;

@Slf4j
public class LogoutSuccessHandlerImpl implements LogoutSuccessHandler {

    @Override
    public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) {
        // LogoutFilter#doFilter()中，logout()没抛异常就会执行onLogoutSuccess()，所以这里要判断前面response是否已经写入了
        // 参考 AbstractAuthenticationTargetUrlRequestHandler，也是判断如果已经写入就不重定向了
        if (!response.isCommitted()) {
            ServletUtils.writeJSON(response, R.success("退出登录成功"));
            log.info("退出登录成功");
        }
    }

}
